package com.liqi.frame.comm.controller;

import com.liqi.frame.comm.Constants;
import com.liqi.frame.comm.dto.HttpResult;
import com.liqi.frame.comm.enums.ResultEnum;
import com.liqi.frame.comm.token.TokenUtil;
import com.liqi.frame.comm.util.ShiroUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by liqi on 2018/2/5.
 * liqiwork@qq.com
 */
@Controller
@RequestMapping("/permission/ajax")
public class PermissionAjaxController {
    @RequestMapping({"hasPermission"})
    @ResponseBody
    public HttpResult hasPermission(@RequestBody String permissionKey) {
        HttpResult result = new HttpResult(ResultEnum.SUCCESS);
        if (TokenUtil.isAdministrator() || ShiroUtil.getSubject().isPermitted(Constants.OPERATION_PASSKEY) || ShiroUtil.getSubject().isPermitted(permissionKey)) {
//        if (TokenUtil.isAdministrator() || SecurityUtils.getSubject().isPermitted(permissionKey)) {
            result.setData(true);
        } else {
            result.setData(false);
        }
        return result;
    }

    @RequestMapping({"isAdministrator"})
    @ResponseBody
    public HttpResult isAdministrator() {
        HttpResult result = new HttpResult(ResultEnum.SUCCESS);
        if (TokenUtil.isAdministrator()) {
            result.setData(true);
        } else {
            result.setData(false);
        }
        return result;
    }

}
